﻿using B.S.DataBase.MyProject.Domain.HealthMonitoring;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.HealthMonitoringCommand;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.HealthMonitoringCommandHandler
{
    public class GetBloodSugarCommandHandler : IRequestHandler<GetBloodSugarCommand, APIResult<List<BloodSugar>>>
    {

        private readonly SqlSugarContext sqlSugar;

        public GetBloodSugarCommandHandler(SqlSugarContext sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }
        public async Task<APIResult<List<BloodSugar>>> Handle(GetBloodSugarCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<List<BloodSugar>> result = new APIResult<List<BloodSugar>>();
                var entity = sqlSugar.db.Queryable<BloodSugar>().Where(x => x.UserId == request.UserId);               
                entity = entity.OrderByDescending(x=>x.SId);
                if (entity != null)
                {
                    result.Code = APIEnum.查询成功;
                    result.Msg = APIEnum.查询成功.ToString();
                    result.data = entity.ToList();
                }
                else
                {
                    result.Code = APIEnum.查询失败;
                    result.Msg = APIEnum.查询失败.ToString();
                }
                return await Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
